/*
 * Obec.java
 *
 * Created on 25. květen 2007, 20:36
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package servissimulator;


import java.io.FileNotFoundException;

import java.io.*;

import java.util.Random;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;

import org.w3c.dom.NamedNodeMap;

import org.w3c.dom.NodeList;

/**
 *
 * @author jirka
 */
public class Obec {
    private Random random;
    private String xmlFileInput = "data/obce.xml";
    /** Creates a new
     * instance of Obec */
    public Obec() {
        random = new Random();
    }
    
    /**
     *@return obec 
     */
     public String getRandomVillage(){
                String  result=null;
		Document doc;
		DocumentBuilder docBuilder;
		DocumentBuilderFactory dBuFa = DocumentBuilderFactory.newInstance();
		try {
			docBuilder = dBuFa.newDocumentBuilder();
			doc = docBuilder.parse(this.xmlFileInput);
			NodeList l = doc.getElementsByTagName("obec");
			NamedNodeMap map;
			if(l!=null){
				map = l.item(random.nextInt(l.getLength())).getAttributes();
				result = map.getNamedItem("nazev").getNodeValue();
			}
			
			//System.out.println("XML file parsed");
		} //TODO
		catch(FileNotFoundException e){
			System.out.println("File not found..exiting");
			System.exit(1);
		}
		
		catch (Exception e) {
			System.out.println(e.getMessage());
			result = "";
		}
	return result;	
	}
    
}
